package com.example.shop.search

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.GoodsEntity
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class SearchViewModel:BaseViewModel() {
    val searchSuccess = MutableLiveData<List<GoodsEntity>>()
    val searchFail = MutableLiveData<String>()

    fun search(keywords:String){
        viewModelScope.launch {
            try {
                val result = apiServer.goods(0,1,100)
                val data = result.data
                //过滤所有数据中商品描述包含指定关键字的数据
                val searchData = data.filter {
                    keywords in it.goods_desc
                }
                searchSuccess.value = searchData
            }catch (e:Exception){
                searchFail.value = e.message
            }
        }
    }
}